package com.fudan.adse.service.impl;

import com.fudan.adse.domain.po.Cinema;
import com.fudan.adse.domain.po.CinemaExample;
import com.fudan.adse.domain.po.Room;
import com.fudan.adse.domain.po.RoomExample;
import com.fudan.adse.domain.vo.CinemaWithDetailVO;
import com.fudan.adse.exception.ApiException;
import com.fudan.adse.mbg.mapper.CinemaMapper;
import com.fudan.adse.mbg.mapper.LocationMapper;
import com.fudan.adse.mbg.mapper.RoomMapper;
import com.fudan.adse.service.CinemaService;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.fudan.adse.api.ResultCode.FAILED;

/**
 * @author zhl, kmx
 */
@Slf4j
@Service
public class CinemaServiceImpl implements CinemaService {
    @Autowired
    private CinemaMapper cinemaMapper;
    @Autowired
    private LocationMapper locationMapper;
    @Autowired
    private RoomMapper roomMapper;

    @Override
    public Cinema addCinema(Cinema cinema) {
        log.info("开始添加电影院");
        int res = cinemaMapper.insert(cinema);
        if (res > 0) {
            log.info("电影院{}添加成功！", cinema.getName());
            return cinema;
        } else {
            log.info("新电影院{}添加失败！", cinema.getName());
            throw new ApiException(FAILED);
        }
    }

    @Override
    public void updateCinema(Cinema cinema) {
        if (cinemaMapper.updateByPrimaryKey(cinema) < 0) {
            throw new ApiException("更新电影院失败");
        }

    }

    @Override
    public void deleteCinema(Integer cinemaId) {
        Cinema cinema = cinemaMapper.selectByPrimaryKey(cinemaId);
        if (cinema != null) {
            cinema.setStatus(false);
            if (cinemaMapper.updateByPrimaryKeySelective(cinema) < 0) {
                throw new ApiException("删除电影院失败");
            }
        }
    }

    @Override
    public List<CinemaWithDetailVO> loadCinemaByLocation(Integer locationId, String locationDetail, Boolean status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        CinemaExample cinemaExample = new CinemaExample();
        CinemaExample.Criteria criteria = cinemaExample.createCriteria();
        if (locationId != null) {
            criteria.andLocationIdEqualTo(locationId);
        }
        if (locationDetail != null) {
            criteria.andLocationDetailLike("%" + locationDetail + "%");
        }
        if (status != null) {
            criteria.andStatusEqualTo(status);
        }
        List<Cinema> cinemas = cinemaMapper.selectByExample(cinemaExample);
        List<CinemaWithDetailVO> ret = new ArrayList<>();
        cinemas.forEach(cinema -> {
            RoomExample example = new RoomExample();
            example.createCriteria().andCinemaIdEqualTo(cinema.getId());
            ret.add(CinemaWithDetailVO.builder()
                    .cinema(cinema)
                    .location(locationMapper.selectByPrimaryKey(cinema.getLocationId()))
                    .rooms(roomMapper.selectByExample(example))
                    .build());
        });
        return ret;
    }

    @Override
    public List<Room> listRoomsById(Integer cinemaId) {
        RoomExample example = new RoomExample();
        example.createCriteria().andCinemaIdEqualTo(cinemaId);
        return roomMapper.selectByExample(example);
    }
}
